﻿using System;
using System.Linq;
using System.Text;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.ArrayListActions.Add
{
	/// <summary>
	/// 添加字典项的Handler
	/// </summary>
	public class AddObjectActionHandler : ArrayListAddActionHandler
    {
        public override void DoRender ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
			var sbValue = new StringBuilder ( );
			base.DoRenderChildNodes ( sbValue, rootParamObject, parentParamObject, context );
			var typeName = sbValue.ToString ( ).Trim ( );
			if ( typeName.IsNullOrEmpty ( ) )
            {
                throw new NullReferenceException ( "类型名称不能为空！" );
            }
			var type = Type.GetType ( typeName );
			var obj = type.Assembly.CreateInstance ( type.FullName );
            if ( obj == null )
            {
                throw new NullReferenceException ( "指定类型未找到！" );
            }
			var list = context.ToList( ArrayListActionHandler.ARRAY_LIST_OBJECT );
            list.Add ( obj );
        }
    }
}
